package org.example.atomictest;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @author zhengdp
 * @Description 字段更新器 测试
 * @Date 2022/11/20 19:44
 * @Created by zhengdp
 */
public class AtomicFieldTest {
    public static void main(String[] args) {
        Student stu = new Student();

        AtomicReferenceFieldUpdater<Student,String> updater = AtomicReferenceFieldUpdater.newUpdater(Student.class,String.class,"name");

        System.out.println(updater.compareAndSet(stu, null, "张三"));
        System.out.println(stu);
    }

    @Data
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    static class Student {
        volatile String name;
    }
}
